ShouXunScene1={}
ShouXunScene1Func={}
ShouXunScene1Func.__index = ShouXunScene1Func
setmetatable(ShouXunScene1, ShouXunScene1Func)

ShouXunScene1Func.getNode = function()
  local l = {}
  setmetatable(l, ShouXunScene1Func)
  l.node = CCBReader:load("ShouXunScene1.ccbi")
  print("ShouXunScene1.getNode", l.node)
  l.currentGeneral = 1
  ShouXunScene1.instance = l
  l.moving = false
  l.node:registerScriptTouchHandler(ShouXunScene1Func.onTouchLayer, false, 0, false)
  l.node:setTouchEnabled(true)
  l:init() 
  return l 
end

ShouXunScene1Func.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ShouXunScene1.getNode().node)
  print("ShouXunScene1.getScene", sc)
  return sc
end

ShouXunScene1Func.init = function(self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)

  self.leftFlags = {}
  self.rightFlags = {}
  self.itemsRemoved = {}
  self.generals = General:getGeneralsByLevel(1)
  
  local bl = TypeCast(self.node:getChildByTag(1021), "CCButton")
  local br = TypeCast(self.node:getChildByTag(1022), "CCButton")
  self.uiStepper = UIStepper:create(bl, br, "onUIStepperChanged", self)
  self.uiStepper:setCount(General:getGeneralCount(1))
  self.uiStepper:setIndex(0)

  --self.currentGeneral = self.generals[1].id
  self:setBagTable()
  self:setButtonState()
end

ShouXunScene1Func.clear = function(self)
  if self.itemsRemoved==nil then return end
  for k,v in pairs(self.itemsRemoved) do
    self.node:getChildByTag(1001):removeChild(v, true)
    self.node:getChildByTag(1002):removeChild(v, true)
    self.node:removeChild(v, true)
  end
  self.itemsRemoved = {}
end

ShouXunScene1Func.setButtonState = function(self)
  local general = General:create(self.currentGeneral)
  self.leftFlags = {}
  for k,v in ipairs(UserData.config.models.DecorateUsedSpace) do
    if v.level>general.generalInfo.l then
      print("#", 1000+k)
      local b = addButtonFromBK(self.node:getChildByTag(1001), 1000+k, "lockbk.png", "", "onClickLockButton", self)
      self.itemsRemoved[#self.itemsRemoved+1] = b
    end 
  end
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d/%d", self.uiStepper.index+1, self.uiStepper.count))
  print("ShouXunScene1Func.setButtonState gid", self.currentGeneral)
  local sp = addSpriteFromBK(self.node, 1031, string.format("%s_100.png", general.generalConfig.asset))
  self.itemsRemoved[#self.itemsRemoved+1] = sp 
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(general.generalConfig.name)
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setColor(Equip.EquipColor[general.generalConfig.quality])
  TypeCast(self.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("Lv%d", general.generalInfo.l))
  --TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString(tostring())
 
  local ds = XunZhangManager.getOnDecorates(self.currentGeneral)
  for k,v in pairs(ds) do
    self.leftFlags[v.p+1] = tonumber(k) 
    local info = UserData.config.models.Decorate[v.d]
    print("##", 1001+v.p)
    local b = addButtonFromBK(self.node:getChildByTag(1001), 1001+v.p, nameFromPath(info.asset), "", "onClickDecorateButton", self)
    b:setTag(tonumber(k))
    b:setTouchEndOnly(false)
    self.itemsRemoved[#self.itemsRemoved+1] = b
  end
end
ShouXunScene1Func.onUIStepperChanged = function(self, index)
  self.currentGeneral = self.generals[index+1].id 
  print("YuanZhenSceneFunc.onUIStepperChanged", index, self.currentGeneral)
  self:clear()
  self:setButtonState()
  self:setBagTable()
end
ShouXunScene1Func.onClickLockButton = function(self, node, event)
end
ShouXunScene1Func.onClickDecorateButton = function(self, node, event)
end
ShouXunScene1Func.onHeChengButton = function(self, node, event)
end
ShouXunScene1Func.setBagTable = function(self)
  self.rightFlags = {}
  local decorate = UserData.userinfo.host.store.decor
  local totalBagSpace = 4 + decorate.bs
  if totalBagSpace>16 then totalBagSpace=16 end
  for i=1, 16 do
    if i>totalBagSpace then
      local b=addButtonFromBK(self.node:getChildByTag(1002), 2000+i, "lockbk.png", "", "onClickLockButton", self)
      self.itemsRemoved[#self.itemsRemoved+1] = b
    end
  end
  for k,v in pairs(decorate.b) do
    self.rightFlags[v.p+1] = tonumber(k)
    local info = UserData.config.models.Decorate[v.d]
    local b=addButtonFromBK(self.node:getChildByTag(1002), 2001+v.p, nameFromPath(info.asset), "", "onClickDecorateButton", self)
    b:setTag(-tonumber(k))
    b:setTouchEndOnly(false)
    self.itemsRemoved[#self.itemsRemoved+1] = b
  end
end

ShouXunScene1Func.onClickDecorateButton = function(self, node, event)
  if self.moving==true then return end
  local decorate = UserData.userinfo.host.store.decor
  local xzindex = node:getTag()
  --local beginPos = 0
  self.xzIndex = xzindex
  if xzindex<0 then
    self.beginPos = decorate.b[tostring(-xzindex)].p
    self.xzid = decorate.b[tostring(-xzindex)].d
  else
    local ds = XunZhangManager.getOnDecorates(self.currentGeneral)
    self.beginPos = ds[tostring(xzindex)].p
    self.xzid = ds[tostring(xzindex)].d
  end
  --print("ShouXunScene1Func.onClickDecorateButton", xzindex, beginPos)
  
  if event==CCControlEventTouchDragExit then
    print("ShouXunScene1Func.onClickDecorateButton touch exit ", self.beginPos, self.xzIndex, self.xzid)
    local info = UserData.config.models.Decorate[self.xzid]  
    local imgName = nameFromPath(info.asset) 
    local sp = CCSprite:create(imgName)
    local x,y = node:getPosition()
    sp:setPosition(node:getParent():convertToWorldSpace(ccp(x,y)))
    self.node:addChild(sp)
    self.movesp = sp
    self.moving = true
  else
    print("ShouXunScene1Func.onClickDecorateButton touched ", event, node:getTag())
    local d = Decorate:create(math.abs(self.xzIndex))
    self.tipLayer = d:createTip()
    self.node:addChild(self.tipLayer)
    self.tipLayer:registerScriptTouchHandler(ShouXunScene1Func.onTouchTipLayer, false, -1111, true)
    self.tipLayer:setTouchEnabled(true)
  end 
end

ShouXunScene1Func.checkMoveIn = function(self, x,y)
  local leftOrRight = -1
  local pos = -1
  local l = self.node:getChildByTag(1002)
  for i=1,16 do
    local b = l:getChildByTag(2000+i)
    local tp = l:convertToNodeSpace(ccp(x,y))
    local rect = ActionUtil.getNodeRect(b)
    if rect:containsPoint(tp) then
      print("ShouXunScene1Func.checkMoveIn right")
      leftOrRight = 2
      pos = i
      break
    end
  end
  local l = self.node:getChildByTag(1001)
  for i=1,8 do
    local b = l:getChildByTag(1000+i)
    local tp = l:convertToNodeSpace(ccp(x,y))
    local rect = ActionUtil.getNodeRect(b)
    if rect:containsPoint(tp) then
      print("ShouXunScene1Func.checkMoveIn left")
      leftOrRight = 1
      pos = i
      break
    end
  end
  print("ShouXunScene1Func.checkMoveIn", leftOrRight, pos, self.leftFlags[pos], self.rightFlags[pos])
  for k,v in pairs(self.leftFlags) do print("#", k,v) end
  for k,v in pairs(self.rightFlags) do print("@", k,v) end
  --local isCombine = false
  if leftOrRight==-1 then
    self.node:removeChild(self.movesp, true)
    self.movesp = nil
  elseif leftOrRight==2 then
    self.node:removeChild(self.movesp, true)
    self.movesp = nil
    if self.rightFlags[pos]~=nil then
      --NetworkRequest.combine(math.abs(self.xzIndex), pos, "onChange", self)
      self:showCombineDialog(self.rightFlags[pos], math.abs(self.xzIndex))
    else
      NetworkRequest.move(math.abs(self.xzIndex), pos-1, "onChange", self)
    end
  elseif leftOrRight==1 then
    if self.leftFlags[pos]~=nil then
      --NetworkRequest.combine(math.abs(self.xzIndex), pos, "onChange", self)
      self:showCombineDialog(self.leftFlags[pos], math.abs(self.xzIndex))
    else
      NetworkRequest.use(math.abs(self.xzIndex), self.currentGeneral, pos-1, "onChange", self)
    end
  end
end
ShouXunScene1Func.showCombineDialog = function(self, master, guest)
  print("ShouXunScene1Func.showCombineDialog", master, guest)
  self.master = master
  self.guest = guest
  local d1 = Decorate:create(master)
  local d2 = Decorate:create(guest)
  local l, xp1, xp2 = d2:calcLevel()
  local title = string.format("%s 将吞噬 %s", d1.itemConfig.name, d2.itemConfig.name)
  local msg = string.format("获得经验 %d", xp1)

  self.dialog = Dialog:create(title, "fa", self)
  self.node:getParent():addChild(self.dialog.node)
  MainScene.scene:removeChild(self.node, true)
 
  --self.node:getParent():removeChild(self.node, true)
  --ShouXunScene1.instance = nil 
end
ShouXunScene1Func.onOk = function(self, node, event)
  self.node:setVisible(true)
  self.node:getParent():removeChild(self.dialog.node, true)
  setTouchEnabledAll(self.node, true)
  NetworkRequest.combine(self.master, self.guest, "onCombineComplete", self) 
end
ShouXunScene1Func.onCombineComplete = function(self, data)
  local res = cjson.decode(data)
  if res.ret == 0 then
    NetworkRequest.syncData(res)
    --self:clear()
    --self:setBagTable()
    --self:setButtonState()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  MainScene.scene:addChild(ShouXunScene1.getNode().node)
  LoadScene:removeLoadingScene()
end

ShouXunScene1Func.onChange = function(self, data)
  self.node:removeChild(self.movesp, true)
  self.movesp = nil
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:clear()
    self:setBagTable()
    self:setButtonState()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end 
end
ShouXunScene1Func.onTouchTipLayer = function(event, x, y)
  if event==CCTOUCHBEGAN then
    if ShouXunScene1.instance.tipLayer~=nil then
      print("onTouchTipLayer clear tip")
      ShouXunScene1.instance.node:removeChild(ShouXunScene1.instance.tipLayer, true)
      ShouXunScene1.instance.tipLayer= nil
    end
  end
  return true
end
ShouXunScene1Func.onTouchLayer = function(event, x, y)
  if event==CCTOUCHBEGAN then
    return true 
  elseif event==CCTOUCHMOVED then
    if ShouXunScene1.instance.movesp then
      ShouXunScene1.instance.movesp:setPosition(x,y)
    end
  elseif event==CCTOUCHENDED then
    ShouXunScene1.instance.moving=false
    if ShouXunScene1.instance.movesp then
      ShouXunScene1.instance:checkMoveIn(x,y)
    end
  end
  return false
end
ShouXunScene1Func.onConfirm = function(self, dlg, confirm)
  if confirm==false then
    MainScene.scene:removeChild(dlg.node, true)
    MainScene.scene:addChild(ShouXunScene1:getNode().node)
  else
    --setTouchEnabledAll(dlg.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.combine(self.master, self.guest, "onCombineComplete", self)
  end
end
ShouXunScene1Func.onGetXunZhangButton = function(self, node, event)
  setTouchEnabledAll(self.node, false)
  self.node:addChild(ShouXunScene.getNode().node)
end
ShouXunScene1Func.onCloseBagWnd = function(self, node, event)
  --setTouchEnabledAll(self.node, false)
  --setTouchEnabledAll(node:getParent():getParent(), true)
  --node:getParent():getParent():removeChild(node:getParent(), true)
  --local l = ShouXunScene1:getNode().node--CCBReader:load("ShouXunScene1.ccbi")
  --self.node:getParent():getParent():addChild(l)
  MainScene.scene:removeChild(node:getParent(), true)
  ShouXunScene1.instance = nil
  --setTouchEnabledAll(MainScene.node, true)
  --MainScene.setState()
  if self.toNode==nil then
    setTouchEnabledAll(MainScene.node, true)
    MainScene.setState()
  else
    if self.callback~=nil then
      self.callback(self.toNode)
    end
  end
end
